home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / TextArea.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  31.1 KB  |  648 lines

  1. /*
  2.  *
  3.  * 28    1/30/97 1:02p Cthrons
  4.  * Call super.setEditable on last line of "setEditable(ProjBinder)"
  5.  *
  6.  * 27    1/24/97 4:27p Tjones
  7.  * Removed 'throws SQLException' from setbinding method
  8.  *
  9.  * 26    1/17/97 3:14p Tjones
  10.  * Modified setBinding method to throw SQLException
  11.  *
  12.  * 25    1/17/97 2:53p Tjones
  13.  * Fixed bug with setEditable
  14.  *
  15.  * 24    12/19/96 1:42p Tjones
  16.  * Bug fix with the setTreatBlankAs method
  17.  *
  18.  * 23    12/16/96 9:45a Tjones
  19.  * Added parameters to the call to setValueFromString.
  20.  * Added setTreatAsBlank method.
  21.  *
  22.  * 22    12/08/96 4:31p Tjones
  23.  * Make notifySetData throw SQLException and use binder.getStringValue
  24.  * method.
  25.  *
  26.  * 21    12/07/96 6:18p Tjones
  27.  * Changed return type of notifySetData to boolean.
  28.  *
  29.  * 20    12/06/96 3:27p Tjones
  30.  * Added additional comments
  31.  *
  32.  * 19    12/06/96 7:30a Tjones
  33.  * Fixed bug with clearing fields for new record.
  34.  *
  35.  * 18    12/05/96 9:03p Cthrons
  36.  * Check for valid current record state before calling isReadable.
  37.  *
  38.  * 17    12/05/96 5:43p Tjones
  39.  * A whole bunch of changes
  40.  *
  41.  * 16    11/27/96 5:24p Tjones
  42.  *
  43.  * 15    11/27/96 4:29p Tjones
  44.  *
  45.  * 14    11/27/96 8:41a Tjones
  46.  *
  47.  * 13    11/26/96 4:49p Tjones
  48.  *
  49.  * 12    11/26/96 4:33p Tjones
  50.  * Added notifySetData()
  51.  *
  52.  * 11    11/25/96 2:39p Tjones
  53.  *
  54.  * 10    11/25/96 1:41p Tjones
  55.  *
  56.  * 9     11/25/96 1:29p Tjones
  57.  * Introduced dynamic edit method
  58.  *
  59.  * 8     11/21/96 3:55p Tjones
  60.  *
  61.  * 7     11/21/96 3:53p Tjones
  62.  * Reinstituted the setBinding method
  63.  *
  64.  * 6     11/14/96 8:22p Cthrons
  65.  * Put in safety code to make sure binder is readable, writable, etc...
  66.  *
  67.  * 5     11/01/96 1:41p Cthrons
  68.  * Process key input.
  69.  *
  70.  * 4     10/25/96 11:14p Kbradle
  71.  * Remove internal setProjBinding routine (no longer needed)
  72.  * Add calls so that the controls get the first record of data during
  73.  * init().
  74.  *
  75.  * 3     10/15/96 7:27p Kbradle
  76.  * Implement lostFocus and gotFocus which compares the values and only
  77.  * sends data to scale if it changed.
  78.  *
  79.  * 2     10/11/96 7:01p Kbradle
  80.  * change LostFocus to lostFocus (case typo)
  81.  *
  82.  * 1     10/03/96 11:59p Cthrons
  83. */
  84.  
  85. package symantec.itools.db.awt;
  86.  
  87. // import java.awt.*;
  88. import java.util.*;
  89. import symjava.sql.*;
  90. import java.lang.*;
  91. import symantec.itools.db.net.*;
  92. import symantec.itools.db.pro.*;
  93.  
  94.     //*************************************************************************//
  95.     //                                                                         //
  96.     //    CLASS: TextArea                                                      //
  97.     //                                                                         //
  98.     //  PURPOSE: A TextArea object is a multi-line area that displays text.    //
  99.     //              It can be bound to a projection in a database.             //
  100.     //              It can be set to allow editing or read-only modes.         //
  101.     //                                                                         //
  102.     // PARAMETERS:                                                             //
  103.     //                                                                         //
  104.     //  RETURNS:                                                               //
  105.     //                                                                         //
  106.     // COMMENTS:                                                               //
  107.     //                                                                         //
  108.     //*************************************************************************//
  109.  
  110. public class TextArea extends java.awt.TextArea implements ProjLink
  111. {
  112.     private ProjBinder m_ProjBinder; // Object used to interact with server
  113.  
  114.     private boolean m_DynamicUpdate = false;
  115.     private boolean m_DynamicUpdateOverride = true;
  116.     private boolean m_BinderDetermines = true;
  117.     private String  m_BinderData;     // Keep in synch with database
  118.     private String  m_ScreenData;    // Keep in synch with screen text
  119.     private int     m_treatBlankAs =0;
  120.  
  121.     //*************************************************************************//
  122.     //                                                                         //
  123.     // FUNCTION: TextArea                                                      //
  124.     //                                                                         //
  125.     //  PURPOSE: Default constructor                                           //
  126.     //                                                                         //
  127.     // PARAMETERS:                                                             //
  128.     //                                                                         //
  129.     //  RETURNS:                                                               //
  130.     //                                                                         //
  131.     // COMMENTS:                                                               //
  132.     //                                                                         //
  133.     //*************************************************************************//
  134.  
  135.     public TextArea()
  136.     {
  137.         super();
  138.         m_ScreenData = new String();
  139.         m_BinderData = new String()
  140.         ;
  141.     }
  142.  
  143.     //*************************************************************************//
  144.     //                                                                         //
  145.     // FUNCTION: TextArea                                                      //
  146.     //                                                                         //
  147.     //  PURPOSE:                                                               //
  148.     //  * Constructs a new TextArea with the specified number of rows and columns.
  149.     //                                                                         //
  150.     // PARAMETERS:                                                             //
  151.     //  * @param rows the number of rows                                       //
  152.     //  * @param cols the number of columns                                    //
  153.     //                                                                         //
  154.     //  RETURNS:                                                               //
  155.     //                                                                         //
  156.     // COMMENTS:                                                               //
  157.     //                                                                         //
  158.     //*************************************************************************//
  159.  
  160.     public TextArea(int rows, int cols)
  161.     {
  162.         super(rows, cols);
  163.         m_ScreenData = new String();
  164.         m_BinderData = new String();
  165.     }
  166.  
  167.     //*************************************************************************//
  168.     //                                                                         //
  169.     // FUNCTION: TextArea                                                      //
  170.     //                                                                         //
  171.     //  PURPOSE:                                                               //
  172.     //  * Constructs a new TextArea with the specified text.                   //
  173.     //                                                                         //
  174.     // PARAMETERS:                                                             //
  175.     //  * @param text the text to be display                                   //
  176.     //                                                                         //
  177.     //  RETURNS:                                                               //
  178.     //                                                                         //
  179.     // COMMENTS:                                                               //
  180.     //                                                                         //
  181.     //*************************************************************************//
  182.  
  183.     public TextArea(String text)
  184.     {
  185.         super(text);
  186.         m_ScreenData = new String();
  187.         m_BinderData = new String();
  188.     }
  189.  
  190.     //*************************************************************************//
  191.     //                                                                         //
  192.     // FUNCTION: TextArea                                                     //
  193.     //                                                                         //
  194.     //  PURPOSE:                                                               //
  195.     //  * Constructs a new TextArea with the specified text and number of columns.
  196.     //    and columns.
  197.     //                                                                         //
  198.     // PARAMETERS:                                                             //
  199.     //  * @param text the text to be displayed                                 //
  200.     //  * @param rows the number of rows                                       //
  201.     //  * @param cols the number of columns                                    //
  202.     //                                                                         //
  203.     //  RETURNS:                                                               //
  204.     //                                                                         //
  205.     // COMMENTS:                                                               //
  206.     //                                                                         //
  207.     //*************************************************************************//
  208.  
  209.     public TextArea(String text, int rows, int cols)
  210.     {
  211.         super(text, rows, cols);
  212.         m_ScreenData = new String();
  213.         m_BinderData = new String();
  214.     }
  215.  
  216.     //*************************************************************************//
  217.     //                                                                         //
  218.     // FUNCTION: init                                                          //
  219.     //                                                                         //
  220.     //  PURPOSE:                                                               //
  221.     //                                                                         //
  222.     // PARAMETERS:                                                             //
  223.     //  @param binder The ProjBinder binder object which this component can use//
  224.     //                to change data at the database server.                   //
  225.     //                                                                         //
  226.     //  RETURNS:                                                               //
  227.     //                                                                         //
  228.     // COMMENTS:                                                               //
  229.     //  Implement the ProjLink interface function, init().                     //
  230.     //  This function is called when the control is first bound to data.       //
  231.     //                                                                         //
  232.     //*************************************************************************//
  233.  
  234.     public void init (ProjBinder binder)
  235.     {
  236.         m_ProjBinder = binder;
  237.         setEditable(m_ProjBinder);
  238.     }
  239.  
  240.     public void setTreatBlankAs(String blank)
  241.     {
  242.         if (new String(blank).toUpperCase().equals("DEFAULT"))
  243.         {
  244.             m_treatBlankAs = RelationView.SETBLANKTODEFAULT;
  245.         }
  246.         else if (new String(blank).toUpperCase().equals("NULL"))
  247.             {
  248.                 m_treatBlankAs = RelationView.SETBLANKTONULL;
  249.             }
  250.             else if (new String(blank).toUpperCase().equals("BLANK"))
  251.             {
  252.                 m_treatBlankAs = RelationView.SETBLANKTOEMPTY;
  253.             }
  254.     }
  255.  
  256.     //*************************************************************************//
  257.     //                                                                         //
  258.     // FUNCTION: setBinding                                                    //
  259.     //                                                                         //
  260.     //  PURPOSE:                                                               //
  261.     //                                                                         //
  262.     // PARAMETERS:                                                             //
  263.     //                                                                         //
  264.     //  RETURNS:                                                               //
  265.     //                                                                         //
  266.     // COMMENTS:                                                               //
  267.     //                                                                         //
  268.     //*************************************************************************//
  269.  
  270.     public void setBinding(RelationView relView, String projection)
  271.     {
  272.         int projectionNumber = 0;
  273.         int columnType = 0;
  274.  
  275.         try
  276.         {
  277.             RelationViewMetaData rvmd = relView.getMetaData();
  278.             projectionNumber = relView.findProjByName(projection);
  279.             columnType = rvmd.getColumnType(projectionNumber);
  280.             if ( (columnType == Types.DATE) ||
  281.                  (columnType == Types.TIME) ||
  282.                  (columnType == Types.TIMESTAMP) )
  283.             {
  284.                 m_DynamicUpdateOverride = false;
  285.             }
  286.             relView.bindProj(projectionNumber, this);
  287.         }
  288.         catch (SQLException Ex)
  289.         {
  290.             raiseException(
  291.             "SQLException from setBinding: "
  292.             + Ex.getMessage());
  293.         }
  294.     }
  295.  
  296.     //*************************************************************************//
  297.     //                                                                         //
  298.     // FUNCTION: setDynamicUpdate                                              //
  299.     //                                                                         //
  300.     //  PURPOSE:                                                               //
  301.     //                                                                         //
  302.     // PARAMETERS:                                                             //
  303.     //                                                                         //
  304.     //  RETURNS:                                                               //
  305.     //                                                                         //
  306.     // COMMENTS:                                                               //
  307.     //                                                                         //
  308.     //*************************************************************************//
  309.  
  310.     public void setDynamicUpdate(boolean update)
  311.     {
  312.         m_DynamicUpdate = update;
  313.     }
  314.  
  315.     //*************************************************************************//
  316.     //                                                                         //
  317.     // FUNCTION: notifyDataChange                                              //
  318.     //                                                                         //
  319.     //  PURPOSE:                                                               //
  320.     //    Implement the ProjLink interface function, notifyDataChange().       //
  321.     //    This function is called when the projection's data changes at        //
  322.     //    the database.                                                        //
  323.     //                                                                         //
  324.     // PARAMETERS:                                                             //
  325.     //  @param binder The ProjBinder binder object which this component can use//
  326.     //                to get or change data at the database server.            //
  327.     //                                                                         //
  328.     //  RETURNS:                                                               //
  329.     //                                                                         //
  330.     // COMMENTS:                                                               //
  331.     //                                                                         //
  332.     //  Step 1: set m_ProjBinder equal to binder object                        //
  333.     //  Step 2: synchronize m_BinderData with binder object                    //
  334.     //  Step 3: synchronize m_ScreenData with current screen                   //
  335.     //  Step 4: if the two are different then                                  //
  336.     //       - set m_ScreenData equal to m_BinderData to avoid recursion       //
  337.     //       - call setText to update screen                                   //
  338.     //  Step 5: enable or disable editing based on binder                      //
  339.     //                                                                         //
  340.     //*************************************************************************//
  341.  
  342.     public void notifyDataChange(ProjBinder binder)
  343.     {
  344.         // STEP 1
  345.         if (binder == null)
  346.         {
  347.             // Raise an exception..
  348.             return;
  349.         }
  350.         m_ProjBinder = binder;
  351.  
  352.         // STEP 2
  353.         try
  354.         {
  355.             if (binder.getRelationView().getCurrentRecordState()
  356.                 != Record.RECSTATE_INVALID)
  357.             {
  358.                 if (binder.isReadable() && !binder.isNull())
  359.                 {
  360.                     m_BinderData = binder.getStringValue();
  361.                 }
  362.                 else
  363.                 {
  364.                     m_BinderData = "";
  365.                 }
  366.             }
  367.             else
  368.             {
  369.                 m_BinderData = "";
  370.             }
  371.         }
  372.         catch (SQLException Ex)
  373.         {
  374.             raiseException(
  375.             "SQLException from TextArea.notifyDataChange: "
  376.             + Ex.getMessage());
  377.         }
  378.         catch (java.io.IOException Ex)
  379.         {
  380.             raiseException(
  381.             "IOException from TextArea.notifyDataChange: "
  382.             + Ex.getMessage());
  383.         }
  384.         if (m_BinderData == null)
  385.         {
  386.             m_BinderData = new String();
  387.         }
  388.  
  389.         // STEP 3
  390.         m_ScreenData = getText();
  391.  
  392.         // STEP 4
  393.         if (!m_BinderData.equals(m_ScreenData))
  394.         {
  395.             m_ScreenData = m_BinderData;
  396.             setText(m_ScreenData);
  397.         }
  398.  
  399.         // STEP 5
  400.         if (m_BinderDetermines)
  401.         {
  402.             setEditable(m_ProjBinder);
  403.         }
  404.     }
  405.  
  406.     //*************************************************************************//
  407.     //                                                                         //
  408.     // FUNCTION: lostFocus                                                     //
  409.     //                                                                         //
  410.     //  PURPOSE: Intercept the event to send data to the binder                //
  411.     //                                                                         //
  412.     // PARAMETERS:                                                             //
  413.     //      @param evt the lost focus event being caught                       //
  414.     //      @param what the object that is losing focus                        //
  415.     //                                                                         //
  416.     //  RETURNS:                                                               //
  417.     //                                                                         //
  418.     // COMMENTS:                                                               //
  419.     //      Call notifySetData since that is the standard way of handling      //
  420.     //      any event that should cause us to send data to the binder.         //
  421.     //                                                                         //
  422.     //*************************************************************************//
  423.  
  424.     public boolean lostFocus(java.awt.Event evt, Object what)
  425.     {
  426.         try
  427.         {
  428.             notifySetData(m_ProjBinder);
  429.         }
  430.         catch (SQLException Ex)
  431.         {
  432.             raiseException(
  433.             "SQLException from TextArea.notifySetData: "
  434.             + Ex.getMessage());
  435.         }
  436.         return super.lostFocus(evt, what);
  437.     }
  438.  
  439.     //*************************************************************************//
  440.     //                                                                         //
  441.     // FUNCTION: notifyInputChange                                             //
  442.     //                                                                         //
  443.     //  PURPOSE: Handle any event that causes us to send data to binder object //
  444.     //                                                                         //
  445.     //  PARAMETERS:                                                            //
  446.     //                                                                         //
  447.     //  RETURNS:                                                               //
  448.     //                                                                         //
  449.     // COMMENTS:                                                               //
  450.     //                                                                         //
  451.     //                                                                         //
  452.     //  Step 1: synchronize m_ScreenData with current screen                   //
  453.     //  Step 2: if m_ScreenData differs from m_BinderData then...              //
  454.     //       - set m_BinderData equal to m_ScreenData to avoid recursion       //
  455.     //       - call m_Binder.setString to update binder                        //
  456.     //                                                                         //
  457.     //*************************************************************************//
  458.  
  459.     boolean notifyInputChanged(String input)
  460.     {
  461.         // STEP 1
  462.         if (!m_ScreenData.equals(input))
  463.         {
  464.             m_ScreenData = input;
  465.         }
  466.  
  467.         // STEP 2
  468.  
  469. try{
  470.         if (!m_BinderData.equals(m_ScreenData)
  471. && (m_ProjBinder.getRelationView().getCurrentRecordState() != Record.RECSTATE_INVALID)
  472. && (m_ProjBinder.getRelationView().getCurrentRecordState() != Record.RECSTATE_DELETED)
  473. && (m_ProjBinder.getRelationView().getCurrentRecordState() != Record.RECSTATE_DB_DELETED))
  474.         {
  475.             m_BinderData = m_ScreenData;
  476.             try
  477.             {
  478.                 if (m_ProjBinder != null && m_ProjBinder.isWritable())
  479.                 {
  480.                         m_ProjBinder.setValueFromString(m_ScreenData, 0, m_treatBlankAs);
  481.                 }
  482.             }
  483.              catch (java.io.IOException Ex)
  484.             {
  485.                 raiseException(
  486.                 "IOException from TextArea.notifyInputChange.setString: "
  487.                 + Ex.getMessage());
  488.                 return false;
  489.             }
  490.         }
  491.          }
  492.         catch (SQLException Ex)
  493.         {
  494.             raiseException(
  495.             "SQLException from TextField.notifyInputChange: "
  496.             + Ex.getMessage());
  497.             return false;
  498.         }
  499.         return true;
  500.     }
  501.  
  502.     //*************************************************************************//
  503.     //                                                                         //
  504.     // FUNCTION: notifySetData                                                 //
  505.     //                                                                         //
  506.     //  PURPOSE: Send data to binder                                           //
  507.     //                                                                         //
  508.     //  PARAMETERS:                                                            //
  509.     //  @param binder The ProjBinder binder object which this component can use//
  510.     //                to change data at the database server.                   //
  511.     //                                                                         //
  512.     //  RETURNS:                                                               //
  513.     //                                                                         //
  514.     // COMMENTS:                                                               //
  515.     //                                                                         //
  516.     //                                                                         //
  517.     //*************************************************************************//
  518.  
  519.     public boolean notifySetData(ProjBinder binder) throws SQLException
  520.     {
  521.         return ( notifyInputChanged(getText()) );
  522.     }
  523.  
  524.     //*************************************************************************//
  525.     //                                                                         //
  526.     // FUNCTION: handleEvent                                                   //
  527.     //                                                                         //
  528.     //  PURPOSE: Respond to any event we want to handle ourselves.             //
  529.     //                                                                         //
  530.     //  PARAMETERS:                                                            //
  531.     //                                                                         //
  532.     //  RETURNS:                                                               //
  533.     //                                                                         //
  534.     // COMMENTS:                                                               //
  535.     // * Handles the event. Returns true if the event is handled and           //
  536.     // * should not be passed to the parent of this component. The default     //
  537.     // * event handler calls some helper methods to make life easier           //
  538.     // * on the programmer.                                                    //
  539.     // * @param evt the event                                                  //
  540.     // * @;see #mouseEnter                                                     //
  541.     // * @see #mouseExit                                                       //
  542.     // * @see #mouseMove                                                       //
  543.     // * @see #mouseDown                                                       //
  544.     // * @see #mouseDrag                                                       //
  545.     // * @see #mouseUp                                                         //
  546.     // * @see #keyDown                                                         //
  547.     // * @see #action                                                          //
  548.     //                                                                         //
  549.     //*************************************************************************//
  550.  
  551.     public boolean handleEvent(java.awt.Event evt) {
  552.         // debugPrint(evt.toString());
  553.         if (m_DynamicUpdate && m_DynamicUpdateOverride)
  554.         {
  555.             try
  556.             {
  557.                 notifySetData(m_ProjBinder);
  558.             }
  559.             catch (SQLException Ex)
  560.             {
  561.                 raiseException(
  562.                 "SQLException from TextArea.notifySetData: "
  563.                 + Ex.getMessage());
  564.             }
  565.         }
  566.         return super.handleEvent(evt);
  567.     }
  568.  
  569.     //*************************************************************************//
  570.     //                                                                         //
  571.     // FUNCTION: setEditable                                                   //
  572.     //                                                                         //
  573.     //  PURPOSE: Enable or disable input                                       //
  574.     //                                                                         //
  575.     //  PARAMETERS:                                                            //
  576.     //  @param binder The ProjBinder binder object which this component can use//
  577.     //                to change data at the database server.                   //
  578.     //                                                                         //
  579.     //  RETURNS:                                                               //
  580.     //                                                                         //
  581.     // COMMENTS:                                                               //
  582.     //                                                                         //
  583.     //*************************************************************************//
  584.  
  585.     void setEditable(ProjBinder binder)
  586.     {
  587.         //Disable input if binder is not writeable
  588.         boolean isWritable = false;
  589.  
  590.         try
  591.         {
  592.             if (binder != null)
  593.             {
  594.                 RelationView rv = binder.getRelationView();
  595.                 if (rv != null && (rv.getCurrentRecordState() != Record.RECSTATE_INVALID))
  596.                 {
  597.                     isWritable = binder.isWritable();
  598.                 }
  599.             }
  600.         }
  601.         catch (SQLException Ex)
  602.         {
  603.             raiseException(
  604.             "SQLException from setEditable: "
  605.             + Ex.getMessage());
  606.         }
  607.         super.setEditable(isWritable);
  608.     }
  609.  
  610.     //*************************************************************************//
  611.     //                                                                         //
  612.     // FUNCTION: setEditable                                                   //
  613.     //                                                                         //
  614.     //  PURPOSE: Set editable attribute                                        //
  615.     //                                                                         //
  616.     //  PARAMETERS:                                                            //
  617.     //                                                                         //
  618.     //  RETURNS:                                                               //
  619.     //                                                                         //
  620.     // COMMENTS:                                                               //
  621.     //                                                                         //
  622.     //*************************************************************************//
  623.  
  624.     public void setEditable(boolean value)
  625.     {
  626.         m_BinderDetermines = value;
  627.         super.setEditable(value);
  628.     }
  629.  
  630.     //*************************************************************************//
  631.     //                                                                         //
  632.     // FUNCTION: raiseException                                                //
  633.     //                                                                         //
  634.     //  PURPOSE:                                                               //
  635.     //                                                                         //
  636.     //  PARAMETERS:                                                            //
  637.     //                                                                         //
  638.     //  RETURNS:                                                               //
  639.     //                                                                         //
  640.     // COMMENTS:                                                               //
  641.     //                                                                         //
  642.     //*************************************************************************//
  643.  
  644.     void raiseException(String text)
  645.     {
  646.         System.out.println(text);
  647.     }
  648. }